FUNCTION_BLOCK LCB_UnitToPos
TITLE = 'LCB_UnitToPos'
//
// This FB uses the transmitted machine parameters and converts a position n the real machine units into a position in [inc]
// nd outputs it at dnSetPosOut_p for further processing within the FB interconnection.
// Versionshistory:
// 1.0 Ersterstellung E.Dittmann

VERSION : '1.0'
AUTHOR  : 'Lenze'
FAMILY  : 'LCB'
NAME : 'UniToPos'
KNOW_HOW_PROTECT

VAR_INPUT
    AxisData                : BLOCK_DB;                             
    rPositionInUnits        : REAL;                                 // Positionvalue in Units
END_VAR

VAR_OUTPUT
    diPosOut_p              : DINT;                                 // Positionvalue in increments
    xLimit                  : BOOL;                                 // Limit Error   
END_VAR

VAR
    dwTemp                  :DWORD;
    rTemp                   :REAL;
    rPosOut_p               :REAL;
END_VAR

        
BEGIN
    dwTemp              :=AxisData.dd68;   // AxisData.rUnitsToPos
    rTemp               :=DWORD_TO_REAL(dwTemp);
    rPosOut_p           :=rPositionInUnits *  rTemp;
    IF  (rPosOut_p >= 2147483647.0)  THEN
        xLimit := TRUE;
        diPosOut_p := 2147483647;
    ELSIF  (rPosOut_p <= -2147483647.0) THEN
        xLimit := TRUE;
        diPosOut_p := -2147483647;
    ELSE
        xLimit := FALSE;
        diPosOut_p := REAL_TO_DINT(rPosOut_p);
    END_IF;

END_FUNCTION_BLOCK


